from django.shortcuts import render

# Create your views here.
from rest_framework.generics import GenericAPIView, ListAPIView, RetrieveAPIView
from rest_framework.mixins import ListModelMixin, RetrieveModelMixin
from rest_framework.viewsets import ReadOnlyModelViewSet
from rest_framework_extensions.cache.mixins import CacheResponseMixin

from .models import Area
from . import serializers
# # GET /areas/
# class AreasView(ListModelMixin,GenericAPIView):
#     def get(self):
#         list()
#         # 查询Area数据，复数序列化器返回
#
# class AreasView(ListAPIView):
#     pass
#
#
# # GET /areas/<pk>/
# class SubAreasView(RetrieveModelMixin, GenericAPIView):
#     def get(self):
#         retrieve()
#     # 查询单一的数据对象序列化器返回
# class SubAreasView(RetrieveAPIView):
#     pass

# class AreasViewSet(ListModelMixin,RetrieveModelMixin,GenericViewSet)


class AreasViewSet(ReadOnlyModelViewSet,CacheResponseMixin):
    # 关闭分页处理
    pagination_class = None


    def get_queryset(self):
        if self.action == 'list':
            return Area.objects.filter(parent=None)
        else:
            return Area.objects.all()

    def get_serializer_class(self):
        if self.action == 'list':
            return serializers.AreaSerializer
        else:
            return serializers.SubAreaSerializer


# /areas/  {'get':'list'} 只返回顶级数据　parent=None
# /areas/<pk>/ {'get':'retrieve'}
# 请求的url    请求方式　　调用的方法



